package com.chenya.modual.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenya.modual.entity.Users;
import com.chenya.modual.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <h1> </h1>
 *
 * @author 90后野生程序猿
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2021/7/6 9:12 PM
 */
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService{

    @Autowired
    private UserMapper userMapper;



    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //调用userMapper中方法进行查询
        QueryWrapper<Users> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_name",username);
        Users users = userMapper.selectOne(userQueryWrapper);
        if(users == null){
            throw new UsernameNotFoundException("用户不存在");
        }
        List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_manager");
        return new User(users.getUserName(),new BCryptPasswordEncoder().encode(users.getPassWord()),auths);
    }
}
